import { Injectable } from '@angular/core';
import { AppConfig } from '../config';
import { Http, Headers } from '@angular/http';
import { DeviceCategory } from '../entity/device-category.model';
import { DeviceChannel } from '../entity/device-channel.model';
import { InterceptorService } from '../service/interceptor/interceptor-service';

@Injectable()
export class DeviceChannelService {
    private param = 'deviceChannels';
    private get url(): string {
        return `${AppConfig.APP_RESTFUL}/${this.param}`;
    }
    constructor(private http: InterceptorService) { }

    getDeviceChannels():Promise<DeviceChannel[]> {
        return this.http.get(this.url)
            .toPromise()
            .then(response => response.json().data as DeviceChannel[])
            .catch(this.handleError);
    }
    getDeviceChannelsByFarmId(farmId: number):Promise<DeviceChannel[]> {
        return this.http.get(`${this.url}/farm/${farmId}`)
            .toPromise()
            .then(response => response.json().data as DeviceChannel[])
            .catch(this.handleError);
    }
    getDeviceChannelsByDeviceId(deviceId: number):Promise<DeviceChannel[]> {
        return this.http.get(`${this.url}/device/${deviceId}`)
            .toPromise()
            .then(response => response.json().data as DeviceChannel[])
            .catch(this.handleError);
    }
    getDeviceChannelsByParentId(parentId: number):Promise<DeviceChannel[]> {
        return this.http.get(`${this.url}/parent/${parentId}`)
            .toPromise()
            .then(response => response.json().data as DeviceChannel[])
            .catch(this.handleError);
    }

    create(deviceChannel: DeviceChannel): Promise<DeviceChannel> {
        return this.http.post(this.url, JSON.stringify(deviceChannel))
            .toPromise()
            .then(res => {
                try {
                    let result = res.json();
                    switch (result.code) {
                        case "200":
                            return result.data as DeviceChannel;
                        case "-1":
                            throw new Error(result.message);
                    }
                }
                catch (error) {
                    this.handleError(error);
                }
            })
            .catch(this.handleError);
    }
    update(deviceChannel: DeviceChannel): Promise<DeviceChannel> {
        const url = `${this.url}`;
        return this.http.put(url, JSON.stringify(DeviceChannel))
            .toPromise()
            .then(() => deviceChannel)
            .catch(this.handleError);
    }
    delete(id: number): Promise<void> {
        const url = `${this.url}/${id}`;
        return this.http.delete(url)
            .toPromise()
            .then(() => null)
            .catch(this.handleError);
    }
    batchDelete(valueIds: number[]): Promise<any> {
        const apiUrl = `${this.url}?items=${valueIds}`;
        return this.http.delete(apiUrl)
            .toPromise()
            .then(response => {
                try {
                    let result = response.json();
                    switch (result.code) {
                        case "200":
                            return result;
                        case "-1":
                            throw new Error(result.message);
                        case "8001":
                            return result;
                    }
                }
                catch (error) {
                    throw error;
                }

            })
            .catch(this.handleError);
    }
    private handleError(error: any): Promise<any> {
        // console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }

}


// WEBPACK FOOTER //
// ./src/app/service/device-channel.service.ts